package cn.xmkeshe.controller;

import cn.xmkeshe.back.IOrderDeServiceBack;
import cn.xmkeshe.front.IOrderDeServiceFront;
import cn.xmkeshe.model.Cars;
import cn.xmkeshe.model.Customer;
import cn.xmkeshe.model.OrderDe;
import cn.xmkeshe.utils.Log;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.*;

@Controller
public class OrderDeController {

  @Resource
  private IOrderDeServiceFront iOrderDeServiceFront;
  @Resource
  private IOrderDeServiceBack iOrderDeServiceBack;

//  申请托运
  @RequestMapping("/pages/front/orderDe/addPre")
  @Log(operationName = "申请托运页面",operationType = "用户")
  public String addPre(){
    return "/pages/front/orderDe/add";
  }

  @RequestMapping("/pages/front/orderDe/add")
  @ResponseBody
  @Log(operationName = "申请托运",operationType = "系统")
  public String add(OrderDe orderDe, HttpServletRequest request){

    String flag = "error";
    orderDe.setOrid(UUID.randomUUID().toString().substring(1,10).replace("-",""));//订单编号
    Customer customer = (Customer) request.getSession().getAttribute("customer");
    String cuid = customer.getCuid();
    orderDe.setCuid(cuid);//保存客户编号
//    对账状态，默认都为0
    orderDe.setCaccount(0);
    orderDe.setAaccount(0);
    orderDe.setOrderdate(new Timestamp(new Date().getTime()));//订单生成日期
    orderDe.setStatus(1);//提交表单
    if(iOrderDeServiceFront.add(orderDe)){
      return "success";
    }
    return flag;
  }

//  查询未审核订单
  @RequestMapping("/pages/back/orderDe/listSplitPre")
  @Log(operationName = "查询未审核订单前",operationType = "司机")
  public String listSplitPre(){
    return "/pages/back/orderDe/listByStatus";
  }

  @RequestMapping("/pages/back/orderDe/listSplit")
  @ResponseBody
  @Log(operationName = "未审核订单",operationType = "系统")
  public Map<String, Object> listSplit(Customer customer, HttpServletRequest request){
    int currentPage = 1;
    int lineSize = 10;
    try{
      currentPage = Integer.parseInt(request.getParameter("page"));
    }catch (Exception e){}
    try{
      lineSize = Integer.parseInt(request.getParameter("limit"));
    }catch (Exception e){}
    Map<String, Object> map = iOrderDeServiceBack.listSplitByStatus(currentPage,lineSize);
    map.put("code",0);//解析接口状态，必须这样写
    map.put("msg","加载中");//解析提示文本
    map.put("data",map.get("allOrderDe"));//    解析数据列表
    map.put("count",map.get("allCount"));//    解析数据长度
    return map;
  }

//  审核订单
  @RequestMapping("/pages/back/orderDe/editOrderDe")
  @ResponseBody
  @Log(operationName = "审核订单",operationType = "司机")
  public String edit(OrderDe orderDe){
    String flag = "error";
    orderDe.setStatus(2);
    orderDe.setDelivery(new Timestamp(new Date().getTime()));
    if(iOrderDeServiceBack.edit(orderDe)){
      return "success";
    }
    return flag;
  }

  //  分页查询前(已审核)
  @RequestMapping("/pages/back/orderDe/listSplitPre1")
  @Log(operationName = "查询已审核订单",operationType = "司机")
  public String listSplitPre1(){
    return "/pages/back/orderDe/listByStatus1";
  }

  @RequestMapping("/pages/back/orderDe/listSplit1")
  @ResponseBody
  @Log(operationName = "已审核订单",operationType = "系统")
  public Map<String, Object> listSplit1(Customer customer, HttpServletRequest request){
    int currentPage = 1;
    int lineSize = 10;
    try{
      currentPage = Integer.parseInt(request.getParameter("page"));
    }catch (Exception e){}
    try{
      lineSize = Integer.parseInt(request.getParameter("limit"));
    }catch (Exception e){}
    Map<String, Object> map = iOrderDeServiceBack.listSplitByStatus1(currentPage,lineSize);
    map.put("code",0);//解析接口状态，必须这样写
    map.put("msg","加载中");//解析提示文本
    map.put("data",map.get("OrderDe"));//    解析数据列表
    map.put("count",map.get("Count"));//    解析数据长度
    return map;
  }

//  司机完结订单
  @RequestMapping("/pages/back/orderDe/editOrderDeStatus")
  @ResponseBody
  @Log(operationName = "完结订单",operationType = "司机")
  public String editOrderDeStatus(String orid){
    String flag = "error";
    if(iOrderDeServiceBack.editOrderDeStatus(orid)){
      return "success";
    }
    return flag;
  }

//  查询已完结订单
  @RequestMapping("/pages/back/orderDe/listSplitPre2")
  @Log(operationName = "查询已完结订单页面",operationType = "管理员/司机")
  public String listSplitPre2(){
    return "/pages/back/orderDe/listByStatus2";
  }

  @RequestMapping("/pages/back/orderDe/listSplit2")
  @ResponseBody
  @Log(operationName = "已完结订单",operationType = "系统")
  public Map<String, Object> listSplit2(Customer customer, HttpServletRequest request){
    int currentPage = 1;
    int lineSize = 10;
    try{
      currentPage = Integer.parseInt(request.getParameter("page"));
    }catch (Exception e){}
    try{
      lineSize = Integer.parseInt(request.getParameter("limit"));
    }catch (Exception e){}
    Map<String, Object> map = iOrderDeServiceBack.listSplitByStatus2(currentPage,lineSize);
    map.put("code",0);//解析接口状态，必须这样写
    map.put("msg","加载中");//解析提示文本
    map.put("data",map.get("OrderDe"));//    解析数据列表
    map.put("count",map.get("Count"));//    解析数据长度
    System.out.println(map);
    return map;
  }


//  客户查询订单
    @RequestMapping("/pages/front/orderDe/listPre")
    @Log(operationName = "查询订单页面",operationType = "用户")
    public String listPre(){
      return "/pages/front/orderDe/list";
}

    @RequestMapping("/pages/front/orderDe/list")
    @ResponseBody
    @Log(operationName = "订单页面",operationType = "系统")
    public Map<String, Object> list(HttpServletRequest request){
      Customer customer = (Customer) request.getSession().getAttribute("customer");
      String cuid = customer.getCuid();
      Map<String,Object> map = new HashMap<String,Object>();
      map.put("code",0);//解析接口状态，必须这样写
      map.put("msg","加载中");//解析提示文本
      map.put("data",iOrderDeServiceFront.findAll(cuid));//    解析数据列表
      map.put("count",100);//    解析数据长度
      return map;
    }

//  订单评价
    @RequestMapping("/pages/front/orderDe/editOrderDe")
    @ResponseBody
    @Log(operationName = "订单评价",operationType = "用户")
    public String comment(OrderDe orderDe, HttpServletRequest request){
    //    orid,cuid,iid,title,number,line,orderdate,status
      String flag = "error";
//      orderDe.setOrid(UUID.randomUUID().toString().substring(1,10).replace("-",""));//订单编号
      Customer customer = (Customer) request.getSession().getAttribute("customer");
      String cuid = customer.getCuid();
      orderDe.setCuid(cuid);//保存客户编号
      String orid = orderDe.getOrid();
      orderDe.setOrid(orid);
      System.out.println(orid);
//      orderDe.setOrderdate(new Timestamp(new Date().getTime()));//订单生成日期
//      orderDe.setStatus(1);//提交表单
      if(iOrderDeServiceFront.comment(orderDe)){
        return "success";
      }
      return flag;
    }

//    客户对账
    @RequestMapping("/pages/back/orderDe/caccountListPre")
    @Log(operationName = "客户对账页面",operationType = "管理员")
    public String caccountListPre(){
      return "/pages/back/account/caccount";
    }

//    司机对账
    @RequestMapping("/pages/back/orderDe/aaccountListPre")
    @Log(operationName = "司机对账页面",operationType = "管理员")
    public String aaccountListPre(){
      return "/pages/back/account/aaccount";
    }

//    查询所有对账信息
    @RequestMapping("/pages/back/orderDe/accountList")
    @ResponseBody
    @Log(operationName = "查询所有对账信息",operationType = "系统")
    public Map<String, Object> listSplit(HttpServletRequest request,OrderDe orderDe){
      int currentPage = 1;
      int lineSize = 10;
      try{
        currentPage = Integer.parseInt(request.getParameter("page"));
      }catch (Exception e){}
      try{
        lineSize = Integer.parseInt(request.getParameter("limit"));
      }catch (Exception e){}
      Map<String, Object> map = iOrderDeServiceBack.findCAccount(currentPage,lineSize);
  //    System.out.println(map);
      map.put("code",0);//解析接口状态，必须这样写
      map.put("msg","加载中");//解析提示文本
      map.put("data",map.get("OrderDe"));//    解析数据列表
      map.put("count",map.get("Count"));//    解析数据长度
      return map;
    }

    @RequestMapping("/pages/back/orderDe/aaccount")
    @ResponseBody
    @Log(operationName = "查询管理员对账",operationType = "管理员")
    public String editAaccount(OrderDe orderDe){
      String flag = "error";
      String orid = orderDe.getOrid();
      orderDe.setOrid(orid);
      orderDe.setAtime(new Timestamp(new Date().getTime()));
      if (iOrderDeServiceBack.editAaccount(orderDe)){
        return "success";
      }
      return flag;
    }

    @RequestMapping("/pages/back/orderDe/caccount")
    @ResponseBody
    @Log(operationName = "查询客户对账",operationType = "管理员")
    public String editCaccount(OrderDe orderDe){
      String flag = "error";
      String orid = orderDe.getOrid();
      orderDe.setOrid(orid);
      orderDe.setCtime(new Timestamp(new Date().getTime()));
      if (iOrderDeServiceBack.editCaccount(orderDe)){
        return "success";
      }
      return flag;
    }

    //模糊查询
    @RequestMapping("/pages/back/orderde/findByOrid")
    @ResponseBody
    @Log(operationName = "模糊查询订单",operationType = "系统")
    public Map<String, Object> findByName(String orid, HttpServletRequest request){
      int currentPage = 1;
      int lineSize = 10;
      try{
        currentPage = Integer.parseInt(request.getParameter("page"));
      }catch (Exception e){}
      try{
        lineSize = Integer.parseInt(request.getParameter("limit"));
      }catch (Exception e){}
      Map<String, Object> map = iOrderDeServiceBack.findByOrid(currentPage,lineSize,"%" + orid + "%");
      map.put("code",0);//解析接口状态，必须这样写
      map.put("msg","加载中");//解析提示文本
      map.put("data",map.get("data"));//    解析数据列表
      map.put("count",map.get("count"));//    解析数据长度
      return map;
    }
}
